package com.ftwj.controller;

import com.ftwj.dto.RegisterRequest;
import com.ftwj.entity.SysUser;
import com.ftwj.service.CaptchaService;
import com.ftwj.service.SysUserManageService;
import com.ftwj.utils.result.MessageEnum;
import com.ftwj.utils.result.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class SysUserManageController {

    @Autowired
    private SysUserManageService sysUserManageService;

    @Autowired
    private CaptchaService captchaService;

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(
            @RequestBody RegisterRequest registerRequest,
            HttpSession session) {
        Map<String, Object> response = new HashMap<>();

        // 验证验证码
        if (!captchaService.validateCaptcha(session, registerRequest.getCaptcha())) {
            response.put("status", "error");
            response.put("message", "验证码错误");
            return ResponseEntity.badRequest().body(response);
        }
        try {
            // 注册用户
            SysUser user = registerRequest.getUser();
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setState(0); // 设置用户状态为待审核
            sysUserManageService.register(user);

            response.put("status", "success");
            response.put("message", "注册成功，请等待审核");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            // 捕获注册过程中的异常并返回错误信息
            response.put("status", "error");
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/login")
    public ResultBean login(@RequestParam String phoneNumber,
                            @RequestParam String password,
                            @RequestParam String captcha,
                            HttpSession session) {
        // 验证验证码
        if (!captchaService.validateCaptcha(session, captcha)) {
            return ResultBean.error(MessageEnum.VERIFICATION_CODE_ERROR);
        }

        // 登录用户
        return sysUserManageService.login(phoneNumber, password);
    }

    @PostMapping("/approveUser")
    public ResponseEntity<Map<String, Object>> approveUser(@RequestParam String id,
                                                           @RequestParam Integer state) {
        SysUser user = sysUserManageService.findUserById(id);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("message", "User not found"));
        }

        user.setState(state); // 1表示批准，2表示拒绝
        sysUserManageService.updateUser(user);

        return ResponseEntity.ok(Map.of("message", "User status updated successfully"));
    }
}
